package com.lhd.web;

import com.lhd.po.Comment;
import com.lhd.po.User;
import com.lhd.service.BlogService;
import com.lhd.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by liuhandong on 2022/4/20.
 */
@RestController
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private BlogService blogService;

    @Value("${comment.avatar}")
    private String avatar;

    @GetMapping("/comment/{id}")
    public List<Comment> getComment(@PathVariable Long id){
        return commentService.listCommentByBlogId(id);
    }

    @PostMapping("/comment/post")
    public Integer postComment(@RequestBody Comment comment, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user != null){
            comment.setAvatar(user.getAvatar());
            comment.setAdminComment(true);
        }else {
            comment.setAvatar("https://unsplash.it/100/100?image=1005");
        }
        return commentService.saveComment(comment);
    }


}
